package views.features;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.sql.SQLException;

import javax.swing.JPanel;

import core.database.DataBaseManager;
import views.util.GameColors;

public class StatisticsView extends FeatureView {

	private IstogramPanel[] istogramPanels;
	private static StatisticsView instance;

	public static StatisticsView newInstance() {
		if (instance == null)
			instance = new StatisticsView();
		return instance;
	}

	private StatisticsView() {
		setLayout(new GridLayout(7, 1));
		istogramPanels = new IstogramPanel[6];
		for (int i = 0; i < 6; i++) {
			istogramPanels[i] = new IstogramPanel(i);
			istogramPanels[i].setBackground(Color.WHITE);
			add(istogramPanels[i]);
		}
		istogramPanels[5].makeRecord();
		add(new JPanel());
	}

	public void initializeValues(int[] values) {

		int maxValue = values[values.length - 1]; // nell'ultima posizione si
													// trova il miglior
													// punteggio
		double partialFraction = (double) 1 / (double) maxValue;
		for (int i = 0; i < 6; i++) {
			istogramPanels[i].setValue(values[i], partialFraction);
			istogramPanels[i].repaint();
		}

	}

	private class IstogramPanel extends JPanel {
		private Color istogramColor;
		private String areaString;
		private int value;
		private double valueFraction;
		private int area;
		private boolean record;

		public IstogramPanel(int areaColorId) {
			area = areaColorId;
			switch (areaColorId) {
			case 0:
				istogramColor = GameColors.AZZURRO;
				areaString = "VELOCITA'";
				break;
			case 1:
				istogramColor = GameColors.VERDE;
				areaString = "MEMORIA";
				break;
			case 2:
				istogramColor = GameColors.ARANCIONE;
				areaString = "ATTENZIONE";
				break;
			case 3:
				istogramColor = GameColors.BORDEAUX;
				areaString = "FLESSIBILITA'";
				break;
			case 4:
				istogramColor = GameColors.VIOLA;
				areaString = "PROBLEM \nSOLVING";
				break;
			case 5:
				istogramColor = GameColors.GRIGIO_SCURO;
				areaString = "MIGLIOR \nPUNTEGGIO";
				break;
			}
		}

		public void makeRecord() {
			record = true;
		}

		public void setValue(double value, double partialFraction) {
			this.value = (int) value;
			this.valueFraction = partialFraction * value;
		}

		public void paintComponent(Graphics g) {
			super.paintComponent(g);
			g.setColor(istogramColor);
			int height = 50;
			Font f = new Font("Segoe UI", Font.PLAIN, 26);
			g.setFont(f);
			g.drawString(areaString, 3, this.getHeight() / 2 + 5);
			int y = this.getHeight() / 2 - (height / 2);
			int width = (int) ((this.getWidth() - 280) * valueFraction);
			g.fillRect(270, y, width, height);
			Font f1 = new Font("Segoe UI", Font.PLAIN, 20);
			g.setFont(f1);
			if (!record)
				g.drawString("La tua media: " + value, 270, y);
			else
				g.drawString("Record personale: " + value, 270, y);
			if (area < 5)
				try {
					int g1 = DataBaseManager.getGlobalRecord(2 * area + 1);
					int g2 = DataBaseManager.getGlobalRecord(2 * area + 2);
					int gg = g1 > g2 ? g1 : g2;
					g.drawString("Record utenti : " + gg,
							this.getWidth() - 260, y);
				} catch (SQLException e) {
					// TODO Auto-generated catch block

				}
		}
	}

}
